package com.gyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyg.constant.CloudConstant;
import com.gyg.constant.RedisConstants;
import com.gyg.domain.CloudSpace;
import com.gyg.domain.CloudUser;
import com.gyg.dao.CloudUserMapper;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.service.CloudSpaceService;
import com.gyg.service.CloudUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.utils.ConverterUtil;
import com.gyg.utils.RandomUtil;
import com.gyg.utils.RedisUtil;
import com.gyg.vo.param.LoginParam;
import com.gyg.vo.param.RegisterParam;
import com.gyg.vo.param.UserParam;
import com.gyg.vo.param.UserPwdParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 用户信息表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-02-20
 */
@Service
@Slf4j
public class CloudUserServiceImpl extends ServiceImpl<CloudUserMapper, CloudUser> implements CloudUserService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CloudSpaceService cloudSpaceService;

    @Override
    public RespEntity checkUsername(String username) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getUsername, username);
        CloudUser cloudUser = baseMapper.selectOne(queryWrapper);
        if (Objects.nonNull(cloudUser)) {
            return new RespEntity(RespCode.REGISTER_ERROR);
        }
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity register(RegisterParam registerParam) {
        if (Objects.isNull(registerParam)) {
            log.info("参数非法或者格式不正确！");
            return new RespEntity(RespCode.PARAMETERS_ERROR);
        }

        String oldCode = redisUtil.get(RedisConstants.key.getSms(registerParam.getMobile()));
        if (! StringUtils.equals(registerParam.getCode(), oldCode)) {
            return new RespEntity(RespCode.CODE_ERROR);
        }

        CloudUser cloudUser = ConverterUtil.copyProperties(registerParam, CloudUser.class);
        //随机生成用户名
        cloudUser.setName(RandomUtil.randomUsername());
        //默认头像地址
        cloudUser.setPicPath(CloudConstant.HEAD_URL);
        //国家默认中国
        cloudUser.setCountry("中国");

        //添加用户表数据
        baseMapper.insert(cloudUser);

        //分配默认空间 1GB(1024MB)
        CloudSpace cloudSpace = CloudSpace.builder()
                .userId(cloudUser.getId())
                .spaceTotalSize("1024MB")
                .spaceTotalByte(1024*1024*1024 + "")
                .spaceUsedSize("0B")
                .spaceUsedByte(0 + "")
                .build();
        cloudSpaceService.save(cloudSpace);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity<CloudUser> login(LoginParam loginParam) {
        if (Objects.isNull(loginParam)) {
            log.info("参数非法或者格式不正确！");
            return new RespEntity(RespCode.PARAMETERS_ERROR);
        }

        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getUsername, loginParam.getUsername())
                .eq(CloudUser::getPassword, loginParam.getPassword());

        CloudUser cloudUser = baseMapper.selectOne(queryWrapper);

        return new RespEntity(RespCode.SUCCESS, cloudUser);
    }

    @Override
    public RespEntity updateUser(UserParam userParam) {
        CloudUser cloudUser = ConverterUtil.copyProperties(userParam, CloudUser.class);
        baseMapper.updateById(cloudUser);
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity<Boolean> checkPassword(UserPwdParam userPwdParam) {
        QueryWrapper<CloudUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudUser::getId, userPwdParam.getId())
                .eq(CloudUser::getPassword, userPwdParam.getPassword())
                .eq(CloudUser::getDeleted, 0);
        CloudUser cloudUser = baseMapper.selectOne(queryWrapper);
        //输入的旧密码不同
        if (Objects.isNull(cloudUser)) {
            return new RespEntity<>(RespCode.SUCCESS, false);
        }
        return new RespEntity<>(RespCode.SUCCESS, true);
    }

    @Override
    public RespEntity updatePassword(UserPwdParam userPwdParam) {
        CloudUser cloudUser = ConverterUtil.copyProperties(userPwdParam, CloudUser.class);
        baseMapper.updateById(cloudUser);
        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity<CloudUser> userInfo(Integer userId) {
        CloudUser cloudUser = baseMapper.selectById(userId);
        return new RespEntity<>(RespCode.SUCCESS, cloudUser);
    }
}
